﻿using Player.Weapons;
using UnityEngine;

public class PlayerBody : HumanBody
{
    public PlayerData PlayerData;

    public PlayerStatus PlayerStatus;

    private PlayerCombatManager combatManager;

    public Transform fire_forward;
    public Transform fire_back;
    public Transform fire_right;
    public Transform fire_left;

    public PlayerBuffManager buffManager;
    public Weapon playerWeapon;

    public float Hp { get; set; }
    public float currentShiled { get; set; }

    public void Init(PlayerData data,int id)
    {
        PlayerData = data;
        ID = id;
    }

    private void Awake()
    {
        ID = 1;
        PlayerData = new PlayerData();
        PlayerStatus = new PlayerStatus();
        combatManager = new PlayerCombatManager(this);
        buffManager = new PlayerBuffManager();
        playerWeapon = Weapon.CreateWeapon("Wand", this);

    }

    public override void ITakeDamage(int id, float offset, float damage, DamageType damageType = DamageType.普通)
    {
        //base.ITakeDamage(id, offset, damage, damageType);
        if (PlayerStatus.isInvincible || PlayerStatus.isInAttacked)
            return;
        if (currentShiled != 0)
        {
            currentShiled -= damage;
        }
        Hp -= damage;

        PlayerStatus.isInAttacked = false;
        PlayerStatus.isInjured = false;
        Invoke("OutOfAttacked", PlayerData.InjuredInterval);
        EventManager.Broadcast(EventName.Player.TakeDamage);

        if (damage <= 0)
        {
            PlayerStatus.isDead = true;
            //EventManager.Broadcast(EventName.Player.Dead);
        }
    }

    public override void ITakeBuff(int id, BuffBase buff)
    {
        base.ITakeBuff(id, buff);
    }

    public override float IHealthRecover(float value)
    {
        //return base.IHealthRecover(value);
        float use = 0f;
        if (Hp >= PlayerData.MaxHp)
        {

            return use;
        }
        else
        {
            use = Recover(Hp, PlayerData.MaxHp, value);
            Hp += use;
            return use;
        }
    }

    public void DataChange(int ID)
    {

    }

    public void OnInvincible(float time)
    {
        PlayerStatus.isInvincible = true;
    }


    private void OutOfAttacked()
    {
        PlayerStatus.isInAttacked = false;
        PlayerStatus.isInjured = false;       
    }

    /// <summary>
    /// 恢复
    /// </summary>
    /// <param name="Start">当前值</param>
    /// <param name="Max">最大值</param>
    /// <param name="Value">增加值</param>
    /// <returns>使用量</returns>
    private float Recover(float Start, float Max, float Value)
    {
        if (Start + Value >= Max)
        {
            return Max - Start;
        }
        else
        {
            return Value;
        }
        
    }

}
